package com.john.bluetoothtest2;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.john.bluetoothtest2.User_Info.userDB;


public class LoginActivity extends AppCompatActivity {
    private long firstTime=0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch(keyCode){
            case KeyEvent.KEYCODE_BACK://点击返回键
                long secondTime = System.currentTimeMillis();//以毫秒为单位
                if(secondTime -firstTime>2000){
                    Toast.makeText(this, "再按一次返回退出程序", Toast.LENGTH_SHORT).show();
                    firstTime=secondTime;
                }else{
                    finish();
                    System.exit(0);
                }
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userDB userDB = new userDB(this);
        userDB.getWritableDatabase();
    }
    public void user_login(View view){
        Intent intent = new Intent(this,HomeActivity.class);
        userDB userDB = new userDB(this);
        SQLiteDatabase db = userDB.getWritableDatabase();
        String account = ((EditText) findViewById(R.id.et1)).getText().toString();
        String password = ((EditText) findViewById(R.id.et2)).getText().toString();
        Cursor cursor = db.query("user_account", null, "phone =? and password=?", new String[]{account, password}, null, null, null);
        if (cursor.getCount() != 0) {
            Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
            intent.putExtra("account",account);
            intent.putExtra("password",password);
            startActivity(intent);
        } else {
            Toast.makeText(this, "账号或密码不正确！", Toast.LENGTH_SHORT).show();
        }
        cursor.close();
        db.close();
    }
}